import http.server
from urllib.parse import urlparse

from html_10_index import handle_root_path
from html_11_select_range import handle_select_range_path
from html_12_practice import handle_practice_path
from html_13_question import handle_question_path
from html_14_fill_blank import handle_fill_blank_path, handle_find_demo_path
from stats_manager import (
    handle_get_stats,
    handle_save_stats,
    handle_reset_stats,
    handle_get_wrong_questions,
    handle_get_last_question,
)


class MyHandler(http.server.SimpleHTTPRequestHandler):
    def __init__(
        self, *args, questions=None, questionFilesOfFill=None, demo_path=None, **kwargs
    ):
        self.questions = questions
        self.questionFilesOfFill = questionFilesOfFill
        self.demo_path = demo_path
        super().__init__(*args, **kwargs)

    def do_GET(self):
        parsed_path = urlparse(self.path)
        if parsed_path.path == "/":
            handle_root_path(self)
        elif parsed_path.path == "/select_range":
            handle_select_range_path(self, parsed_path)
        elif parsed_path.path == "/practice":
            handle_practice_path(self, parsed_path)
        elif parsed_path.path.startswith("/question/"):
            handle_question_path(self, parsed_path)
        elif parsed_path.path == "/get_stats":
            handle_get_stats(self)
        elif parsed_path.path == "/get_wrong_questions":
            handle_get_wrong_questions(self)
        elif parsed_path.path == "/get_last_question":
            handle_get_last_question(self)
        elif parsed_path.path == "/fill_blank":
            handle_fill_blank_path(self, parsed_path)
        elif parsed_path.path == "/find_demo":
            handle_find_demo_path(self, parsed_path)
        else:
            self.send_error(404)

    def do_POST(self):
        if self.path == "/save_stats":
            handle_save_stats(self)
        elif self.path == "/reset_stats":
            handle_reset_stats(self)
        else:
            self.send_error(404)
